<template>
  <div class="app-container">
    <!--    0人工审核中，1审核通过，-1不通过，2未发布，3机器审核中-->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="用户名" width="110">
        <template slot-scope="scope">
          {{ scope.row.user.account }}
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" width="150">
        <template slot-scope="scope">
          {{ scope.row.user.nickname }}
        </template>
      </el-table-column>

      <el-table-column label="用户头像" width="200" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.user.avatar"
            :preview-src-list="[scope.row.user.avatar]"
          />
        </template>
      </el-table-column>
      <el-table-column label="本周收益" width="50">
        <template slot-scope="scope">
          {{ scope.row.weekEarnings }}
        </template>
      </el-table-column>

      <el-table-column label="总收益" width="150">
        <template slot-scope="scope">
          {{ scope.row.totalEarnings }}
        </template>
      </el-table-column>

      <el-table-column label="是否是管理员" width="150">
        <template slot-scope="scope">
          {{ scope.row.isAdmin? '是':'否' }}
        </template>
      </el-table-column>

      <el-table-column label="加入时间" width="100">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="110" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" round @click="dialogFormVisible = true; associationUser.userId=scope.row.userId">
            设置权限
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :page-size="size"
      :current-page="page"
      :total="total"
      @current-change="fetchData"
    />

    <el-dialog title="设置权限" :visible.sync="dialogFormVisible">
      <el-radio v-model="associationUser.role" label="1">管理员</el-radio>
      <el-radio v-model="associationUser.role" label="2">普通成员</el-radio>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSetRole">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getUserList,setRole } from '@/api/party'
import { getUser } from '@/utils/auth'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      status: '-10',
      page: 1,
      dialogFormVisible: false,
      size: 10,
      associationUser: {
        role: 0,
        userId: 0
      },
      total: 0
    }
  },
  created() {
    this.fetchData(this.page)
  },
  methods: {
    handleClick(tab, event) {
      this.fetchData(this.page)
    },
    fetchData(val) {
      this.page = val
      this.listLoading = true
      const user = getUser()
      const json = JSON.parse(user)
      getUserList({ status: 1, page: this.page, size: this.size, phone: json.phone }).then(response => {
        this.total = response.total
        console.log(this.page)
        this.list = response.data
        this.listLoading = false
      })
    },
    handleSetRole() {
      setRole(this.associationUser).then(res => {
        this.fetchData(this.page)
        this.dialogFormVisible = false
      })
    },
    handelRealDelete(id) {
      this.$confirm('此操作将彻底删除该动态,请谨慎操作！', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        realDeleteById(id).then(() => {
          this.fetchData(this.page)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
